package org.example;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.List;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-persistence-unit");
        EntityManager em = emf.createEntityManager();
        nativeQuery(em,"SHOW TABLES");
        nativeQuery(em,"SHOW COLUMNS FROM EMPLOYEE");
        nativeQuery(em,"SHOW COLUMNS FROM FULL_TIME_EMPLOYEE");
        nativeQuery(em,"SHOW COLUMNS FROM PART_TIME_EMPLOYEE");
        persistEmployee(em);
        nativeQuery(em,"SELECT * FROM EMPLOYEE");
        nativeQuery(em,"SELECT * FROM FULL_TIME_EMPLOYEE");
        nativeQuery(em,"SELECT * FROM PART_TIME_EMPLOYEE");
        em.close();
    }
    private static void persistEmployee(EntityManager em){
        FullTimeEmployee e1 = new FullTimeEmployee();
        e1.setName("Sara");
        e1.setSalary(10000);

        PartTimeEmployee e2 = new PartTimeEmployee();
        e2.setName("Tom");
        e2.setHourlyRate(60);

        em.getTransaction().begin();
        em.persist(e1);
        em.persist(e2);
        em.getTransaction().commit();
    }
    private static void nativeQuery(EntityManager em,String s){
        System.err.println("--------------" + s + "------------------");
        Query query = em.createNativeQuery(s);
        List list = query.getResultList();
        for (Object o : list) {
            if (o instanceof Object[]) {
                System.err.println(Arrays.toString((Object[])o));
            }else{
                System.err.println(o);
            }
        }
    }
}
